package org.lzy.shop.activity


import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import org.lzy.shop.R
import org.lzy.shop.adapter.CouponAdapter
import org.lzy.shop.base.BaseMvvmActivity
import org.lzy.shop.databinding.ActivityCouponlistBinding
import org.lzy.shop.viewmodel.CouponModel

/**
 * 优惠劵
 */

class CouponListActivity : BaseMvvmActivity<ActivityCouponlistBinding, CouponModel>({ ActivityCouponlistBinding.inflate(it) }){
    private var mPageIndex = 1
    // 使用lazy委托避免空指针问题
    private val couponAdapter: CouponAdapter by lazy { CouponAdapter() }

    private val FIRST_PAGE = 1
    private var isLoadingMore = false
    private var hasMoreData = true
    private var isRefreshing = false


    override fun initView() {
        setTitleText(getResString(R.string.my_coupon))

        // 设置下拉刷新
        binding.refreshLayout.apply {
            setOnRefreshListener {
                refreshData()
            }
            // 设置刷新颜色
            setColorSchemeResources(R.color.colorPrimary)
        }

        binding.recyclerview.apply {
            layoutManager = LinearLayoutManager(this@CouponListActivity)
            adapter = couponAdapter
            setItemViewCacheSize(10)  // 减少缓存大小，根据实际情况调整
            setHasFixedSize(true)
            // 添加滚动监听，实现加载更多
            addOnScrollListener(object : RecyclerView.OnScrollListener() {
                override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                    super.onScrolled(recyclerView, dx, dy)

                    val layoutManager = recyclerView.layoutManager as LinearLayoutManager
                    val visibleItemCount = layoutManager.childCount
                    val totalItemCount = layoutManager.itemCount
                    val firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition()

                    // 当滚动到底部且有更多数据且不在加载中时，触发加载更多
                    if (visibleItemCount + firstVisibleItemPosition >= totalItemCount &&
                        firstVisibleItemPosition >= 0 &&
                        hasMoreData &&
                        !isLoadingMore &&
                        !isRefreshing &&
                        totalItemCount >= 10) { // 假设每页10条数据
                        loadMoreData()
                    }
                }
            })
        }
    }



    override fun initData() {
        mViewModel.myCouponData.observeWithLifecycle(this@CouponListActivity){couponResponse ->
            // 确保无论数据如何都停止刷新
            finishRefresh()

            couponResponse?.let {
                val list = it.list.orEmpty()
                if (list.isNotEmpty()) {
                    if (mPageIndex == 1) {
                        couponAdapter.submitList(list)
                    } else {
                        couponAdapter.addAll(list)
                    }
                    // 判断是否还有更多数据
                    hasMoreData = list.size >= 10 // 假设每页10条数据
                } else {
                    if (mPageIndex == 1) {
                        showListEmpty()
                    } else {
                        hasMoreData = false
                        // 可以在这里显示没有更多数据的提示
                    }
                }
            }
        }
    }

    override fun requestData() {
        getMyCoupon()
    }

    private fun refreshData() {
        mPageIndex = FIRST_PAGE
        hasMoreData = true
        // 确保刷新状态为true
        isRefreshing = true
        getMyCoupon()
    }

    private fun finishRefresh() {
        // 重置加载状态
        isLoadingMore = false
        isRefreshing = false

        // 停止刷新动画
        binding.refreshLayout.isRefreshing = false
    }


    private fun loadMoreData() {
        if (hasMoreData && !isLoadingMore) {
            isLoadingMore = true
            mPageIndex++
            getMyCoupon()
        }
    }

    fun getMyCoupon(){
        mViewModel.getMyCoupon(mPageIndex)
    }

    override fun allClick() {}


    fun showListEmpty() {
        couponAdapter.submitList(null)
        couponAdapter.isStateViewEnable = true
        couponAdapter.stateView = showEmptyView()
    }
}